package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.jpush.JPushs;
import top.jnaw.jee.utils.nim.NIMMsg;

/**
 * Copyright (C) 2018 版权所有。 <p> 文件名： 文件功能描述：
 *
 * @author lc  2018/8/23 16:07 创建内容： <p> <p> 修改标识： 修改原因：
 */
public class MessagePush {

  private String title;
  private String alert;
  private String from;
  private String type;
  private String from_id;
  private String type_id;
  private String announcement_type;
  private List<Users> userList;


  public MessagePush(String title, String alert, String from,
      String type, String from_id, String type_id,
      String announcement_type, List<Users> userList) {
    this.title = title;
    this.alert = alert;
    this.from = from;
    this.type = type;
    this.from_id = from_id;
    this.type_id = type_id;
    this.announcement_type = announcement_type;
    this.userList = userList;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getAlert() {
    return alert;
  }

  public void setAlert(String alert) {
    this.alert = alert;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getFrom_id() {
    return from_id;
  }

  public void setFrom_id(String from_id) {
    this.from_id = from_id;
  }

  public String getType_id() {
    return type_id;
  }

  public void setType_id(String type_id) {
    this.type_id = type_id;
  }

  public String getAnnouncement_type() {
    return announcement_type;
  }

  public void setAnnouncement_type(String announcement_type) {
    this.announcement_type = announcement_type;
  }

  public List<Users> getUserList() {
    return userList;
  }

  public void setUserList(List<Users> userList) {
    this.userList = userList;
  }

  public void sendJPush() {
    sendJPush(false);
  }

  public void sendJPush(boolean local) {
    Map<String, String> extras = new HashMap<>();
    if (StrKit.notBlank(from)) {
      extras.put("from", from);
    }
    if (StrKit.notBlank(type)) {
      extras.put("type", type);
    }
    if (StrKit.notBlank(from_id)) {
      extras.put("from_id", from_id);
    }
    if (StrKit.notBlank(type_id)) {
      extras.put("type_id", type_id);
    }
    if (StrKit.notBlank(announcement_type)) {
      extras.put("announcement_type", announcement_type);
    }
    JPushs.sendjpush(local, title, alert, userList, extras);
  }

  public void sendNIM() {
    sendNIM(false);
  }

  public void sendNIM(boolean local) {
    JSONArray userArray = new JSONArray();
    for (Users user : userList) {
      userArray.add(user.getUsername());
    }
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();

    object.put("type", "push");

    data.put("title", title);
    data.put("content", alert);
    if (StrKit.notBlank(type_id)) {
      data.put("type_id", type_id);
    }
    if (StrKit.notBlank(type)) {
      data.put("type", type);
    }
    if (StrKit.notBlank(from)) {
      data.put("from", from);
    }
    if (StrKit.notBlank(from_id)) {
      data.put("from_id", from_id);
    }
    if (StrKit.notBlank(type_id)) {
      data.put("type_id", type_id);
    }
    if (StrKit.notBlank(announcement_type)) {
      data.put("announcement_type", announcement_type);
    }

    object.put("data", data);

    // [Neo] .TODO check admin role, id = 1
    Users admin = null;
    if (!local) {
      String username = Shiros.getUser();
      if (null != username) {
        admin = Users.getByUsername(username);
      }
    }

    if (null == admin) {
      admin = Users.dao.findById(1);
    }
    NIMMsg.sendBatch(admin, userArray.toJSONString(), object);
  }
}
